﻿using System;
using FileMonitor.Service.Prototype;
using FileMonitor.Service.Tracking.Manager;

namespace FileMonitor
{
   public  class ProcessManager
    {
         private static bool s_Initialized;
        private static Exception s_InitializeException;
        private static object s_lock;
        private static FileProcessProviderCollection s_Processes;

        public static FileProcessProviderCollection Processes
        {
            get
            {
                Initialize();
                return s_Processes;
            }
        }

        static ProcessManager()
        {
            s_lock = new object();
            s_Initialized = false;
            s_InitializeException = null;
        }

        private static void Initialize()
        {
            if (s_Initialized)
            {
                if (s_InitializeException != null)
                {
                    throw s_InitializeException;
                }
            }
            else
            {
                if (s_InitializeException != null)
                {
                    throw s_InitializeException;
                }

                lock (s_lock)
                {
                    if (s_Initialized)
                    {
                        if (s_InitializeException != null)
                        {
                            throw s_InitializeException;
                        }
                    }
                    else
                    {
                        try
                        {

                            s_Processes = FileMonitor.Service.Provider.ProcessProvider.ProcessProvider.GetAllProcess();
                            LockProcesses();
                        }
                        catch (Exception e)
                        {
                            s_InitializeException = e;
                            throw;
                        }
                        s_Initialized = true;
                    }

                }

            }

        }
        private static void LockProcesses()
        {
            try
            {
                foreach (FileMonitorBase p in s_Processes)
                {
                    FileMonitor.Service.Provider.ProcessProvider.ProcessProvider.LockProcess(p);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        internal static void StartProcesses()
        {
            FileMonitor.Service.TimerTrriger.Trriger.InitializeTrigger();
            try
            {
                foreach (FileMonitorBase p in Processes)
                {
                    p.Start();
                }
                TrackingService.CurrentTrackingManager.StartTracking();
                FileMonitor.Service.TimerTrriger.Trriger.StartTriggerTimer();
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        internal static void StopProcesses()
        {
            try
            {
                foreach (FileMonitorBase p in Processes)
                {
                    p.Stop();
                }
                s_Initialized = false;
                TrackingService.CurrentTrackingManager.StopTracking();
                FileMonitor.Service.TimerTrriger.Trriger.StopTriggerTimer();
            }
            catch (Exception ex)
            {
                throw;
            }
        }


    }
}
